package com.sibu.yx.promotion.admin.web.support.model;

import com.sibu.yx.promotion.admin.web.support.filter.UidToken;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.Arrays;
import java.util.List;

/**
 * @author     ：yuwenbo
 * @date       ：Created in 2019/4/17 18:01
 * @description： 通过token验证登陆的
 */
@Deprecated
public class TokenShiroRealm extends AuthorizingRealm {


    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof UidToken;
    }


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        List<String> principalsList = principals.asList();
        //通过principals获取角色与权限
        SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
        authorizationInfo.addRoles(Arrays.asList("admin","service"));
        authorizationInfo.addStringPermissions(Arrays.asList("user:update","user:delete","pay:money"));
        authorizationInfo.addObjectPermission(new BitPermission(5));//添加可读可修改
        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UidToken uid=(UidToken) token;
        //TODO 查询用户信息,验证是否有效用户
        return new SimpleAuthenticationInfo(uid.getPrincipal(),uid.getCredentials(),getName());
    }

}
